package com.maysham.coder.server.dao;

import com.maysham.coder.server.model.Group;
import com.maysham.coder.server.model.UserGroup;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

@Repository
public class GroupDao extends BaseDao<Group> {
    @Resource
    private MongoTemplate template;

    public Group create(Group group) {
        return template.insert(group);
    }

    public List<UserGroup> findUserGroups(String userId) {
        return template.find(Query.query(Criteria.where("user.id").is(userId)), UserGroup.class);
    }

    public Collection<UserGroup> addUserGroups(Collection<UserGroup> groups) {
        return template.insertAll(groups);
    }
}
